package com.leave.controller;

import java.util.HashMap;
import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leave.util.DateUtil;
import com.leave.entity.Leave;
import com.leave.entity.User;
import com.leave.service.ILeaveService;
import com.leave.service.IUserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class LeaveController {
	@Autowired
	private ILeaveService leaveService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/addLeave")
	@ResponseBody
	public String addLeave(Leave leave) {
		User u = (User)SecurityUtils.getSubject().getPrincipal();
		leave.setLeaveUserid(u.getUserId());
		leave.setLeaveState("未提交");
		int i = leaveService.addLeave(leave);
		if(i > 0) {
			return "success";
		}else {
			return "false";
		}
	}
	
	@RequestMapping("/getLeaveByPage")
	@ResponseBody
	public String getLeaveByPage(Integer page,Integer rows) {
		User u = (User)SecurityUtils.getSubject().getPrincipal();
		HashMap<String,Object> map = leaveService.getLeaveByPage(page, rows, u.getUserId());
		List<Leave> leaveList = (List<Leave>) map.get("leaveList");
		JSONArray jsArr = new JSONArray();
		for(int i = 0;i<leaveList.size();i++) {
			JSONObject js = new JSONObject();
			js.put("leaveId", leaveList.get(i).getLeaveId());
			js.put("leaveTime", DateUtil.formatDate(leaveList.get(i).getLeaveTime(), "yyyy-MM-dd HH:mm"));
			js.put("leaveDay", leaveList.get(i).getLeaveDay());
			js.put("leaveReason", leaveList.get(i).getLeaveReason());
			js.put("leaveState", leaveList.get(i).getLeaveState());
			jsArr.add(js);
		}
		JSONObject js = new JSONObject();
		js.put("total", map.get("total"));
		js.put("rows", jsArr);
		return js.toString();
	}
	
	/**
	 * 开启流程并完成提交任务
	 * @param id
	 * @return
	 */
	@RequestMapping("/startAppley")
	@ResponseBody
	public String startAppley(int id) {
		HashMap<String, Object> processVar = new HashMap<String,Object>();
		processVar.put("leaveId", id);
		
		List<User> userList = userService.getUserListByRole("项目组长");
		String zuzhangId = "";
		for(int i = 0;i < userList.size();i++) {
			zuzhangId += userList.get(i).getUserId();
			if(i != userList.size()-1) {
				zuzhangId += ',';
			}
		}
		processVar.put("zuzhang", zuzhangId);
		
		//根据流程名来获取流程实例并且开启流程，并存入流程变量
		ProcessInstance pi = runtimeService.startProcessInstanceByKey("myProcess",processVar);
		//根据流程实例id来获取任务实例
		Task task=taskService.createTaskQuery().processInstanceId(pi.getProcessInstanceId()).singleResult(); 
		//根据任务id来完成任务
		taskService.complete(task.getId());
		Leave leave = leaveService.getLeaveByLeaveId(id);
		leave.setLeaveState("审核中");
		leave.setLeaveProcessinstanceid(pi.getProcessInstanceId());
		int i = leaveService.updateLeaveByLeave(leave);
		if(i == 1) {
			return "success";
		}
		return "false";
	}
	
	
	
	
	
	
	
}
